package com.distribution.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.distribution.common.core.controller.BaseController;
import com.distribution.common.core.domain.AjaxResult;
import com.distribution.common.core.page.TableDataInfo;
import com.distribution.common.exception.ServiceException;
import com.distribution.framework.config.ServerConfig;
import com.distribution.system.domain.entity.CmccDaDistanceFlow;
import com.distribution.system.domain.entity.CmccDaProduct;
import com.distribution.system.domain.req.CmccDaDistanceFlowReq;
import com.distribution.system.domain.req.CmccDaProductListReq;
import com.distribution.system.service.CmccDaProductService;
import com.distribution.system.service.CmccDaDistanceFlowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: GodLu
 * @create: 2024-04-09 10:09
 * @description: 异地分流
 */
@RestController
@RequestMapping("/cmccDaDistanceFlow")
@Slf4j
public class CmccDaDistanceFlowController extends BaseController {

    @Autowired
    CmccDaProductService cmccDaProductService;

    @Autowired
    CmccDaDistanceFlowService cmccDaDistanceFlowService;

    @PostMapping("/getDistanceInfoListByProductId")
    public List<CmccDaDistanceFlow> getDistanceInfoListByProductId(@RequestBody Integer productId){
        //System.out.println("请求参数：" + id);
        return cmccDaDistanceFlowService.getDistanceInfoListByProductId(productId);
    }

    /**
     * 异地分流商品列表查询
     *
     * @return 商品列表
     */
    @PostMapping("/distanceFlowProductList")
    public TableDataInfo distanceFlowProductList(@RequestBody CmccDaProductListReq cmccDaProductListReq) {
        List<CmccDaProduct> productListByQueryParam = cmccDaProductService.getProductListByQueryParam(cmccDaProductListReq);
        List<CmccDaDistanceFlow> cmccDaDistanceFlowList = cmccDaDistanceFlowService.list();
        //根据cardid去重
        List<Integer> idList = cmccDaDistanceFlowList.stream().map(CmccDaDistanceFlow::getProductId).collect(Collectors.toList());
        List<CmccDaProduct> cmccDaProductList = productListByQueryParam.stream().filter(x -> idList.contains(x.getId())).collect(Collectors.toList());
        if (cmccDaProductList.isEmpty()) throw new ServiceException("查询不到分流信息");
        startPage();
        return getDataTable(cmccDaProductList);
    }

    /**
     * 添加异地分流商品
     *
     * @param cmccDaDistanceFlowReq 异地分流商品请求实例
     * @return 请求状态
     */
    @PostMapping("/add")
    AjaxResult add(@RequestBody @Valid CmccDaDistanceFlowReq cmccDaDistanceFlowReq) {
        cmccDaDistanceFlowService.addDistanceFlowInfo(cmccDaDistanceFlowReq.getCurrentId(), cmccDaDistanceFlowReq.getDistanceFlowId());
        return success();
    }

    /**
     * 取消分流
     *
     * @param productId 分流商品id
     * @return 请求状态
     */
    @PostMapping("/cancelDistanceFlowByProductId")
    AjaxResult cancelDistanceFlowByProductId(@RequestBody Integer productId) {
        //System.out.println(productId);
        List<CmccDaDistanceFlow> list = cmccDaDistanceFlowService.list(new LambdaQueryWrapper<CmccDaDistanceFlow>().eq(CmccDaDistanceFlow::getProductId, productId));
        if (list == null || list.isEmpty()) return error("数据不存在");
        List<Integer> idList = list.stream().map(CmccDaDistanceFlow::getId).collect(Collectors.toList());
        cmccDaProductService.lambdaUpdate().eq(CmccDaProduct::getId, productId).set(CmccDaProduct::getDistanceFlowFlag, "0").update();
        return cmccDaDistanceFlowService.removeByIds(idList) ? success() : error("删除失败");
    }

    /**
     * 删除分流商品
     *
     * @param id ID
     * @return 请求状态
     */
    @PostMapping("/delDistanceFlowInfoById")
    AjaxResult del(@RequestBody Integer id) {
        //删除指定id的分流商品
        return cmccDaDistanceFlowService.removeById(id) ? success() : error("删除失败");
    }

}
